//导入
import Vue from 'vue'
import VueRouter from 'vue-router'
//一级
import layout from '@/views/layout'
import login from '@/views/login'
import pay from '@/views/pay'
import search from '@/views/search'
import searchList from '@/views/search/list.vue'
import prodetail from '@/views/prodetail'
import myorder from '@/views/myorder'
//二级
import home from '@/views/layout/home.vue'
import category from '@/views/layout/category.vue'
import cart from '@/views/layout/cart.vue'
import user from '@/views/layout/user.vue'
import store from '@/store'
//注册使用
Vue.use(VueRouter)

const routes = [
  {
    path: '/', component: layout, redirect: '/home',
    children: [

      { path: '/home', component: home },
      { path: '/category', component: category },
      { path: '/cart', component: cart },
      { path: '/user', component: user },
    ]
  },
  { path: '/login', component: login, },
  { path: '/myorder', component: myorder },
  { path: '/pay', component: pay },
  { path: '/myorder', component: myorder },
  { path: '/search', component: search },
  { path: '/searchlist/:value?', component: searchList },
  { path: '/prodetail/:id', component: prodetail },


]

const router = new VueRouter({
  mode: 'history',
  routes
})
const authUrls = ['/pay', '/myorder']
router.beforeEach((to, from, next) => {
  if (!authUrls.includes(to.path)) {
    next()
    return
  }
  const token = store.getters.token
  console.log(token);
  if (token) {
    next()
  } else {
    next('/login')
  }
})
export default router
